package com.bitcola.security.admin.rest;

import cn.hutool.core.util.ObjectUtil;
import com.bitcola.security.admin.biz.MenuBiz;
import com.bitcola.security.admin.biz.UserBiz;
import com.bitcola.security.admin.entity.Menu;
import com.bitcola.security.admin.entity.User;
import com.bitcola.security.admin.rpc.service.PermissionService;
import com.bitcola.security.common.msg.AppResponse;
import com.bitcola.security.common.msg.TableResultResponse;
import com.bitcola.security.common.rest.BaseController;
import com.bitcola.security.common.util.AdminQuery;
import com.bitcola.security.admin.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author wx
 * @create 2017-06-08 11:51
 */
@RestController
@RequestMapping("user")
public class UserController extends BaseController<UserBiz,User> {
    @Autowired
    private PermissionService permissionService;

    @Autowired
    private MenuBiz menuBiz;

    @RequestMapping(value = "/front/info", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<?> getUserInfo(String token) throws Exception {
        FrontUser userInfo = permissionService.getUserInfo(token);
        if(userInfo==null) {
            return ResponseEntity.status(401).body(false);
        } else {
            return ResponseEntity.ok(userInfo);
        }
    }

    @RequestMapping(value = "/front/menus", method = RequestMethod.GET)
    public @ResponseBody
    List<MenuTree> getMenusByUsername(String token) throws Exception {
        return permissionService.getMenusByUsername(token);
    }

    @RequestMapping(value = "/front/menu/all", method = RequestMethod.GET)
    public @ResponseBody
    List<Menu> getAllMenus() throws Exception {
        return menuBiz.selectListAll();
    }


    @RequestMapping(value = "/pages",method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<UserVO> queryByPages(@RequestParam Map<String, Object> params){
        params.put("condition",params.get("condition"));
        //查询列表数据
        AdminQuery query = new AdminQuery(params);

        return baseBiz.page(query);
    }

    /**
     * 修改资金密码
     * @param id
     * @return
     */
    @RequestMapping(value = "/cipher/{id}" , method = RequestMethod.PUT)
    @ResponseBody
    public Boolean updateCipher(@PathVariable("id") String id){
        //修改资金密码
        return  baseBiz.updateCipher(id);
    }

    /**
     * 修改登录密码
     * @param id
     * @return
     */
    @RequestMapping(value = "/pwd/{id}" , method = RequestMethod.PUT)
    @ResponseBody
    public Boolean updatePassword(@PathVariable("id") String id){
        //修改资金密码
        return  baseBiz.updatePassword(id);
    }

    /**
     * 个人信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/info/{id}" , method = RequestMethod.PUT)
    @ResponseBody
    public UserVO findUserInfo(@PathVariable("id") String id){
        //获取个人信息
        return  baseBiz.findUserInfo(id);
    }

    /**
     * 用户个人资产
     * @param params
     * @return
     */
    @RequestMapping(value = "/assets" , method = RequestMethod.GET)
    @ResponseBody
    public AppResponse findUserAssets(@RequestParam Map<String, Object> params){
        //查询资产
        AdminQuery query = new AdminQuery(params);
        //返回
        AssetsVO vo = baseBiz.findUserAssets( query);
        if(ObjectUtil.isNotEmpty( vo)){
            //统计
            List<AssetsStatisticsVO> list = baseBiz.assetsStatistics( query);
            //遍历
            for(int idx = 0; idx <list.size(); idx ++){
                AssetsStatisticsVO statistics = list.get( idx);
                switch (statistics.getAction()){
                    //充值
                    case "Withdraw" :
                        vo.setWithdraw( statistics.getAmount());
                        break;
                    //提现
                    case "Deposit" :
                        vo.setDeposit( statistics.getAmount());
                        break;
                    //手续费
                    case "feeOut" :
                        vo.setFee( statistics.getAmount());
                        break;
                    //交易 -- 支出
                    case "tradeOut" :
                        vo.setTradeOut( statistics.getAmount());
                        break;
                    //交易 -- 收入
                    case "tradeIn" :
                        vo.setTradeIn( statistics.getAmount());
                        break;
                    //系统奖励
                    case "System reward" :
                        vo.setSysReward( statistics.getAmount());
                        break;
                    //活动奖励
                    case "Activity reward" :
                        vo.setActReward( statistics.getAmount());
                        break;
                    //划转 -- 支出
                    case "transformOut" :
                        vo.setTransformOut( statistics.getAmount());
                        break;
                    //划转 -- 收入
                    case "transformIn" :
                        vo.setTransformIn( statistics.getAmount());
                        break;
                    //锁仓 -- 支出
                    case "unLockOut" :
                        vo.setUnLockOut( statistics.getAmount());
                        break;
                    //锁仓 -- 收入
                    case "unLockIn" :
                        vo.setUnLockIn( statistics.getAmount());
                        break;
                }
            }
        }
        return AppResponse.ok().data( vo);
    }

    /**
     * 资产流水
     * @param params
     * @return
     */
    @RequestMapping(value = "/assets/flow",method = RequestMethod.GET)
    @ResponseBody
    public TableResultResponse<AssetsFlowVO> findAssetsFlow(@RequestParam Map<String, Object> params){
        //查询列表数据
        AdminQuery query = new AdminQuery(params);
        //返回
        return baseBiz.findAssetsFlow( query);
    }

    /**
     *  资产统计
     * @param params
     * @return
     */
    @RequestMapping(value = "/assets/statistics" , method = RequestMethod.GET)
    @ResponseBody
    public List<AssetsStatisticsVO> assetsStatistics(@RequestParam Map<String, Object> params){
        //查询列表数据
        AdminQuery query = new AdminQuery(params);
        //返回
        return baseBiz.assetsStatistics( query);
    }

    /**
     * 团队信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/team/{id}" , method = RequestMethod.PUT)
    @ResponseBody
    public TeamVO findTeam(@PathVariable("id") String id){
        //获取个人信息
        return  baseBiz.findTeam(id);
    }

    /**
     * 权限禁止
     * @param data
     * @return
     */
    @RequestMapping(value = "/auth/limit" , method = RequestMethod.POST)
    @ResponseBody
    public Boolean limit(@RequestBody Map<String, Object> data){
        //操作权限
        return  baseBiz.limit( data);
    }

    /**
     * 权限禁止
     * @param id
     * @return
     */
    @RequestMapping(value = "/limit/{id}" , method = RequestMethod.PUT)
    @ResponseBody
    public List<String> findUserLimit(@PathVariable("id") String id){
        return baseBiz.findLimitList( id);
    }
}
